package com.ruoyi.app.home;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.home.entity.LoveMessage;
import com.ruoyi.app.home.entity.MinioFile;
import com.ruoyi.app.home.mapper.MinioFileMapper;
import com.ruoyi.app.home.mapper.SysLoveMessageMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DeepSeekUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 若依app首页后端接口
 */
@RestController
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private SysLoveMessageMapper loveMessageMapper;

    @Autowired
    DeepSeekUtil deepSeekUtil;
    @Autowired
    MinioFileMapper minioFileMapper;

    /**
     * 生成每日情话
     * @return
     */
    @RequestMapping("/daily/qh")
    public AjaxResult dailyQh() {
        HashMap<String,String> data = new HashMap<>();
        String loveMessageText = deepSeekUtil.generateLoveMessage();
        data.put("message", loveMessageText);

        return success(data);
    }

    /**
     * 生成我们的相册
     * @return
     */
    @RequestMapping("/daily/photo")
    public AjaxResult photo() {
        QueryWrapper<MinioFile> queryWrapper = new QueryWrapper<>();

        // 如果有记录最近使用的图片，可以排除它
        // queryWrapper.ne("id", lastUsedId);

        List<MinioFile> allImages = minioFileMapper.selectList(queryWrapper);
        if (allImages.isEmpty()) {
            return AjaxResult.error("没有找到图片文件");
        }

        // 随机选择一张图片
        MinioFile randomImage = allImages.get(new Random().nextInt(allImages.size()));

        HashMap<String,String> data = new HashMap<>();
        data.put("url", randomImage.getPreviewUrl());
        data.put("fileName", randomImage.getFileName());
        return success(data);
    }

}